package layer
{
	import map.data.Front;
	
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import view.FrontView;

	public class FrontLayer extends BaseLayer
	{
		private var _fronts:Vector.<FrontView>;
		private var _currentSelected:FrontView;
		public function FrontLayer()
		{
			super();
			_fronts = new Vector.<FrontView>();
		}
		
		public function addFront(front:Front):void
		{
			var file:File = new File(front.nativePath);
			var frontview:FrontView = new FrontView(file);
			addChild(frontview);
			frontview.x = front.position.x;
			frontview.y = front.position.y;
			frontview.addEventListener(MouseEvent.CLICK,onClick);
			frontview.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			frontview.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			_fronts.push(frontview);
		}
		
		private function onClick(event:MouseEvent):void
		{
			if(_optionType == MapEditor.SELECT)
			{
				for each(var front:FrontView in _fronts)
				{
					front.selected = false;
				}
				front = event.currentTarget as FrontView;
				front.selected = true;
				_currentSelected = front;
			}
		}
		
		private function onMouseDown(event:MouseEvent):void
		{
			if(_optionType == MapEditor.SELECT)
			{
				var front:FrontView = event.currentTarget as FrontView;
				front.startDrag();
			}
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			var front:FrontView = event.currentTarget as FrontView;
			front.stopDrag();
		}
		
		override public function getData():*
		{
			var result:Vector.<Front> = new Vector.<Front>();
			for each(var fv:FrontView in _fronts)
			{
				result.push(fv.front);
			}
			return result;
		}
		
		public function get fronts():Vector.<FrontView>
		{
			return _fronts;
		}
		
		override public function deleteCurrentSelected():void
		{
			if(_currentSelected)
			{
				_currentSelected.removeEventListener(MouseEvent.CLICK,onClick);
				_fronts.splice(_fronts.indexOf(_currentSelected),1);
				removeChild(_currentSelected);
			}
			_currentSelected = null;
		}
	}
}